package cn.edu.gduf.luowowo.ask.service.impl;

import cn.edu.gduf.luowowo.ask.domain.Question;
import cn.edu.gduf.luowowo.ask.domain.QuestionContent;
import cn.edu.gduf.luowowo.ask.domain.QuestionTag;
import cn.edu.gduf.luowowo.ask.mapper.QuestionContentMapper;
import cn.edu.gduf.luowowo.ask.mapper.QuestionMapper;
import cn.edu.gduf.luowowo.ask.mapper.QuestionTagMapper;
import cn.edu.gduf.luowowo.ask.service.IQuestionService;
import cn.edu.gduf.luowowo.common.query.QueryObject;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

@Service
public class IQuestionServiceImpl implements IQuestionService {
    @Autowired
    private QuestionMapper questionMapper;
    @Autowired
    private QuestionTagMapper questionTagMapper;
    @Autowired
    private QuestionContentMapper questionContentMapper;

    @Override
    public void saveOrUpdate(Question question, String tags) {
        QuestionContent content = question.getContent();
        if (content != null && content.getContent().length() > 200) {
            question.setSummary(content.getContent().substring(0, 200));
        } else {
            question.setSummary(content.getContent());
        }
        question.setLastUpdateTime(new Date());
        if (question.getId() == null) {//新增
            question.setCreateTime(new Date());
            questionMapper.insert(question);
            content.setId(question.getId());
            questionContentMapper.insert(content);
        } else {//修改
            questionMapper.updateByPrimaryKey(question);
            content.setId(question.getId());
            questionContentMapper.updateByPrimaryKey(content);
            questionMapper.removeRelation(question.getId());
        }
        insertTagRelation(question.getId(), tags);
    }

    @Override
    public PageInfo listAll(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<Question> list=questionMapper.selectForList(qo);
        return new PageInfo(list);
    }

    @Override
    public Question selectByPrimaryKey(Long id) {
        return questionMapper.selectByPrimaryKey(id);
    }

    private void insertTagRelation(Long questionId, String tags) {
        List<String> tagsName = questionTagMapper.selectAllName();
        String[] strings = tags.split(",");
        if (strings != null && strings.length > 0) {
            for (String string : strings) {
                if (tagsName.contains(string)) {//标签数据库已经存在
                    QuestionTag questionTag = questionTagMapper.selectByName(string);
                    questionMapper.insertTagRelation(questionId, questionTag.getId());
                } else {//
                    QuestionTag tag = new QuestionTag();
                    tag.setName(string);
                    questionTagMapper.insert(tag);
                    questionMapper.insertTagRelation(questionId, tag.getId());
                }
            }
        }
    }

    @Override
    public void updateQuestionForStatis(Question question) {
        questionMapper.updateQuestionForStatis( question);
    }
}
